In this article, I will show you how to dynamically manage xml configuration file c# asp.net. To configure webconfig file dynamically, open web config file using webconfigurationManager and check the key is exists or not. If appsetting is not configured , add a new key otherwise update the appsetting.
Code behind C#:
protected void Page_Load(object sender, EventArgs e)
{
UpdateSetting("UnobtrusiveJavaScriptEnabled", "true");
}
public void UpdateSetting(string key, string value)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
if (config.AppSettings.Settings[key] == null)
{
config.AppSettings.Settings.Add(key, value);
}
else
{
config.AppSettings.Settings[key].Value= value;
}
config.Save();
ConfigurationManager.RefreshSection("appSettings");
}
Description: Run the application, it will update the following property “UnobtrusiveJavaScriptEnabled” set to true and save the config file and then refreshes the section in the web.config file.
Post your comments / questions
Recent Article
- FieldError: Cannot resolve keyword 'id' into field in Django project
- How to hide the ID field from the Django admin?
- It is impossible to add a non nullable field without specifying a default. Django error
- ImportError: cannot import name 'url' from 'django.conf.urls' - Django Error
- How to Enable Virtualization in BIOS Security Settings in Intel Processors For Android Studio?
- Dependency 'androidx.activity:activity:1.8.0' requires libraries and applications that depend on it.
- AttributeError: 'NoneType' object has no attribute 'get_text' - Python
- ModuleNotFoundError: No module named 'openpyxl' - Python
Related Article